# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: peeweep <peeweep at 0x0 dot ee>

pkgname=neovide
pkgver=0.11.2
pkgrel=4
pkgdesc='No Nonsense Neovim Client in Rust'
arch=(loong64 x86_64)
url="https://github.com/$pkgname/$pkgname"
license=(MIT)
depends=(gcc-libs
         glibc
         fontconfig libfontconfig.so
         freetype2 libfreetype.so
         hicolor-icon-theme
         libglvnd
         neovim
         sndio)
makedepends=(cargo
             cmake)
optdepends=('vulkan-intel: vulkan support for intel'
            'libxkbcommon-x11: run on X11 (not needed for wayland)')
_archive=("$pkgname-$pkgver")
source=("$url/archive/$pkgver/$_archive.tar.gz")
sha256sums=('62e973a5407a6bfc731ce78e0495d2ed10930d33b22fe94cfe23acccbf789ae9')

prepare() {
	cd "$_archive"
	sed -r -i \
		-e '/^incremental/a opt-level = 3' \
		-e '/wayland/s/\]/, "embed-icudtl"]/g' \
		-e '/wayland/a no-default-features = true' \
		Cargo.toml
	cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
	cd "$_archive"
	# this uses malloc_usable_size, which is incompatible with fortification level 3
	# upstream tracking issue https://github.com/neovide/neovide/issues/2040
	export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
	export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
	CFLAGS+=' -ffat-lto-objects'
	export SKIA_USE_SYSTEM_LIBRARIES=true
	cargo build --frozen --release --features embed-fonts
}

package() {
	cd "$_archive"
	install -Dm0755 -t "$pkgdir/usr/bin/" "target/release/$pkgname"
	install -Dm0644 -t "$pkgdir/usr/share/applications/" "assets/$pkgname.desktop"
	for px in 16 32 48 256; do
		install -Dm0644 "assets/$pkgname-${px}x${px}.png" \
			"$pkgdir/usr/share/icons/hicolor/${px}x${px}/apps/$pkgname.png"
	done
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE
}
